<!--
 * @FilePath: 时间函数.html
 * @Author: 苏苏少年
 * @Date: 2022-12-05 20:11:11
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button onclick="toBaidu()">三秒钟跳转百度</button>
    <br>
    <hr>

    <button onclick="stat()">开始</button>
    <button onclick="stop()">暂停</button>
    <h2 id="time"></h2>
</body>
<script>
    function toBaidu() {
        setTimeout(function(){
            window.open("http://www.baidu.com")
        },3000)
    }

    var id;
    function stat(){
        var time = new Date().toLocaleTimeString();
        // 将时间赋值给h2标签.
        document.getElementById("time").innerHTML = time;
        id = setTimeout(stat,1000);
    }

    // 暂停
    function stop(){
        clearTimeout(id);
    }
</script>

</html>